package com.linkoog.devtools.http.everywhere;

import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.NavigationItem;
import com.linkoog.devtools.http.HttpPluginIcons;
import com.linkoog.devtools.http.raw_index.HttpServiceClass;
import com.linkoog.devtools.http.raw_index.HttpServiceMethod;
import com.linkoog.devtools.metadata.DocumentMetadata;
import com.linkoog.devtools.utils.StringUtils;
import lombok.Getter;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class DocSearchItem implements NavigationItem {

    @Getter
    private final DocumentMetadata<?> metaData;
    @Getter
    private final Icon icon;
    private final String name;
    private final String type;
    @Getter
    private final String locationString;
    @Getter
    private final String tailString;



    public DocSearchItem(String name, String type, DocumentMetadata<?> metaData) {
        this.name = name;
        this.type = type;
        this.metaData = metaData;
        this.icon = buildIcon(metaData);
        this.locationString = buildLocationString(metaData);
        this.tailString = buildTailString(metaData);
    }


    @Nullable
    public String getName() {
        return name;
    }


    @Nullable
    public ItemPresentation getPresentation() {
        return null;
    }

    public void navigate(boolean requestFocus) {
        metaData.navigate(requestFocus);
    }

    public boolean canNavigate() {
        return true;
    }

    public boolean canNavigateToSource() {
        return true;
    }

    public Icon buildIcon(DocumentMetadata<?> metaData) {
        if (metaData instanceof HttpServiceClass){
            return HttpPluginIcons.BEAN_ICON;
        } else if (metaData instanceof HttpServiceMethod httpServiceMethod){
            return httpServiceMethod.getHttpMethods().get(0).getIcon();
        }
        return null;
    }


    public String buildLocationString(DocumentMetadata<?> metaData) {
        StringBuilder builder = new StringBuilder();
        if (metaData instanceof HttpServiceClass httpServiceClass){
            if ("url".equals(type)){
                if (StringUtils.isNotBlank(httpServiceClass.getSwaggerComment())){
                    builder.append(httpServiceClass.getSwaggerComment());
                }
            }else if ("swagger".equals(type)){
                builder.append(httpServiceClass.getName());
            } else if ("javaDoc".equals(type)){
                builder.append(httpServiceClass.getName());
            }
        } else if (metaData instanceof HttpServiceMethod httpServiceMethod){
            if ("url".equals(type)){
                if (StringUtils.isNotBlank(httpServiceMethod.getSwaggerComment())){
                    builder.append(httpServiceMethod.getSwaggerComment());
                }
            }else if ("swagger".equals(type)){
                builder.append(httpServiceMethod.getMappingUrls().get(0));
            } else if ("javaDoc".equals(type)){
                builder.append(httpServiceMethod.getMappingUrls().get(0));
            }
        }
        return builder.toString();
    }


    public String buildTailString(DocumentMetadata<?> metaData) {
        StringBuilder builder = new StringBuilder();
        if (metaData instanceof HttpServiceClass httpServiceClass){

            builder.append("Java: (")
                    .append(httpServiceClass.getPsiElement().getName())
                    .append(")");
        } else if (metaData instanceof HttpServiceMethod httpServiceMethod){
            builder.append("Java: (")
                    .append(httpServiceMethod.getClassName())
                    .append("#")
                    .append(httpServiceMethod.getName()).append(")");
        }
        return builder.toString();
    }

}